const express = require('express')
const router = express.Router()

const { login } = require('../controllers/user')

const { findUser } = require('../services/user')

const { decoded } = require('../utils')

const Result = require('../controllers/Result')

router.post('/login', login)

router.get('/info', (req, res, next) => {

  const decode = decoded(req)
  if (decode && decode.username) {
    findUser(decode.username).then(user => {
      console.log(user)

      if (user) {
        user.roles = [user.role]
        new Result(user, '用户信息查询成功').success(res)
      } else {
        new Result('用户信息查询失败').fail(res)
      }
    })
  } else {
    new Result('用户信息查询失败').fail(res)
  }
})


module.exports = router